import 'package:flutter/material.dart';

class NavDemo extends StatelessWidget {
  const NavDemo({
    super.key,
    this.title = '',
  });

  final String title;

  // 重写build 返回需要封装的组件即可
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // home: FirstRoute(),
      routes: {
        // 首页
        '/': (context) => const FirstRoute(),
        // 根据名字跳转到对应的页面
        '/second': (context) => const SecondRoute(),
      },
    );
  }
}

class FirstRoute extends StatelessWidget {
  const FirstRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Route'),
      ),
      body: Center(
        child: ElevatedButton(
          child: const Text('Open route'),
          onPressed: () {
            // 去页面B
            // Navigator.push(
            //   context,
            //   // Material 风格的路由组件
            //   MaterialPageRoute(builder: (context) => const SecondRoute()),
            // );

            // 根据名字进行跳转
            Navigator.pushNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  const SecondRoute({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Route'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // 返回页面 A
            Navigator.pop(context);
          },
          child: const Text('Go back!'),
        ),
      ),
    );
  }
}
